// pages/projectList/projectList.js
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    url: null,
    title: '',
    canUplod: null,//是否可以上拉加载
    dataArray: [],
    sectionType: '', //list  contribute
    srcactionflag:'',
    keywords:'',
    showSerial:false,//是否显示编号 投稿情况下显示
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.type == 0){
      //专题列表
      this.setData({
        url: '/api/v1/knowledge/webview/topic/' + options.kgeId + '/',
        sectionType: options.type,
        showSerial:false
      })
      //标题
      wx.setNavigationBarTitle({
        title: options.title
      });
    }else{
      //投稿作品
      this.setData({
        url: util.getAcceptWorksList,
        sectionType: options.type,
        srcactionflag: options.kgeId,
        showSerial:true
      })
      //标题
      wx.setNavigationBarTitle({
        title: '查看投稿作品（0）'
      });
    }
    console.log('当前url是:' + this.data.url);
    this.getDetailLists();
  },
  search: function (event) {
    console.log('搜索关键字是' + event.detail.value);
    this.setData({
      page: 1,
      keywords: event.detail.value,
    });
    this.getDetailLists();
  },
  //点击查看详情
  viewFilmDetail: function (e) {
    //强转成字符串
    var idStr = String(e.currentTarget.dataset.id);
    var titleStr = String(e.currentTarget.dataset.title);
    console.log('查看作品' + idStr);
    wx.navigateTo({
      url: `../knowledge/knowledge?kgeId=${idStr}&title=${titleStr}`
    })
  },
  //下拉刷新
  onPullDownRefresh: function () {
    console.log('下拉刷新')
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.setData({
      page: 1
    });
    this.getDetailLists();
  },

  //上拉加载
  onReachBottom: function () {
    console.log('上拉加载');
    if (this.data.canUplod) {
      this.getDetailLists();
    } else {
      wx.showToast({
        title: '已经到底了',
        icon: 'none'
      })
    }
  },
  getDetailLists: function () {
    var that = this;
    var tempDic;
    if(that.data.sectionType == 0){
      tempDic = {
        page: that.data.page,
        page_size_count: 12,
      };
    }else{
      tempDic = {
        page: that.data.page,
        page_size_count: 12,
        key_words: that.data.keywords,
        src_action_flag: that.data.srcactionflag,
      };
    }
    util.publicRequestWithToken(
      that.data.url,
      'GET',
      tempDic,
      //sucess
      function (data) {
          if (that.data.page === 1) {
            //清空数据
            that.setData({
              dataArray: []
            })
          }
          var newArr = that.data.dataArray.concat(data.detail.results)
          if (that.data.sectionType == 1){
            //标题
            wx.setNavigationBarTitle({
              title: '查看投稿作品（' + data.detail.count + '）'
            });
          }
          if (data.detail.next) {
            that.setData({
              canUplod: true,
              page: that.data.page + 1,
              dataArray: newArr
            })
          } else {
            that.setData({
              canUplod: false,
              page: that.data.page,
              dataArray: newArr
            })
          }
      },
      //fail
      function (error) {
        wx.showToast({
          title: '网络开小差了',
          icon: 'none'
        })
      },
      function () {
        // complete
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }

    );
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})